package com.erpanderp.wam.dbmigration.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.dbmigration.model.entity.Employeemaster;
import com.erpanderp.wam.dbmigration.model.entity.FlexEmployeeLeave;
import com.erpanderp.wam.dbmigration.model.entity.FlexNewJoinee;
import com.erpanderp.wam.dbmigration.model.entity.FlexUser;
import com.erpanderp.wam.dbmigration.model.entity.ResourceUtilMaster;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
import com.erpanderp.wam.usm.model.entity.user.NewJoinee;

import com.erpanderp.wam.util.HibernateUtil;

public class DataMigrationEngineDAO {

	public List<FlexNewJoinee> getFlexNewJoinee() {
		System.out.println("IN DATA IMPORT DAO");
		List<FlexNewJoinee> flexNewJoineesList = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexNewJoinee f";
			Query query = session.createQuery(queryString);
			flexNewJoineesList = (List<FlexNewJoinee>) query.list();
			session.getTransaction().commit();
			return flexNewJoineesList;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}

	public void creatWamNewJoinee(List<NewJoinee> newJoineesList) {
		System.out.println("IN DATA EXPORT DAO");

		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for (NewJoinee newJoinee : newJoineesList) {
				session.save(newJoinee);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();

			// TODO: handle exception
		}
	}

	public Employee getEmployeeByFlexEmployeeName(String name) {
        System.out.println("IN DATA EXPORT DAO");
        name = "'%" + name + "%'";
        name = name.replaceAll(" ", "%','%");
        String names[] = name.split(",");

        String superVisorName = name;
        try {
            Session session = HibernateUtil.getSessionFactory()
                    .getCurrentSession();
            session.setFlushMode(FlushMode.AUTO);
            session.beginTransaction();
            String queryString = "from Employee e where (e.firstName like " + names[0];
                    for(int i = 1; i < names.length; i++){
                        queryString += " or e.firstName like " + names[i];
                    }
                    queryString += ") and (e.lastName like " + names[0];
                    for(int i = 1; i < names.length; i++){
                        queryString += " or e.lastName like " + names[i];
                    }
                    queryString += ")";

                   
            System.out.println(queryString);
            List<Employee> tempList = (List<Employee>) session.createQuery(queryString).setCacheable(false).list();
            session.getTransaction().commit();

            if (tempList != null && tempList.size() > 0)
                return tempList.get(0);

        } catch (Exception e) {
            e.printStackTrace();

            return null;
        }
        return null;
    }

	public List<Employeemaster> getFlexEmployeeMasters() {
		List<Employeemaster> flexEmployeeMastersList = null;
		System.out.println("IMPORTING EMPLOYEE MASTER");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from Employeemaster as em";
			Query query = session.createQuery(queryString);
			flexEmployeeMastersList = (List<Employeemaster>) query.list();

			System.out.println("FLEX EMPLOYEE MASTERS LIST SIZE IS "
					+ flexEmployeeMastersList.size());
			return flexEmployeeMastersList;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}
	
	public FlexUser getFlexUser(String EmpId){
		System.out.println("IN DATA IMPORT DAO");
		
		List<FlexUser> flexUsers = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from FlexUser f where f.EmpId=:EmpId";
			Query query = session.createQuery(queryString).setString("EmpId", EmpId);
			flexUsers =  (List<FlexUser>) query.list();
			
			if(flexUsers != null && flexUsers.size() > 0){
				return flexUsers.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;			
		}
		return null;
	}

	public void creatWamEmployeeAndWamUser(List<Employee> wamEmployeesList) {
		System.out.println("IN DATA EXPORT DAO");
		System.out.println("WAM EMPLOYEE LIST SIZE " + wamEmployeesList.size());
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for (Employee employee : wamEmployeesList) {
				session.save(employee);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();

			// TODO: handle exception
		}
	}

	public List<ResourceUtilMaster> getResourceUtilMasters() {
		List<ResourceUtilMaster> resourcesUtilList = null;
		System.out.println("IMPORTING RESOURCE EMPLOYEE MASTERS");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from ResourceUtilMaster as rum";
			Query query = session.createQuery(queryString);
			resourcesUtilList = (List<ResourceUtilMaster>) query.list();

			System.out.println("FLEX EMPLOYEE MASTERS LIST SIZE IS "
					+ resourcesUtilList.size());
			return resourcesUtilList;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}

	}

	public Employee getCenterHeadByEmployeeId(String employeeCode) {
		System.out.println("IN EMPLOYEE BY EMPLOYEEID DATA IMPORTING CH");
		Employee employee = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from Employee e where e.employeeCode =:employeeCode";
			Query query = session.createQuery(queryString).setString(
					"employeeCode", employeeCode);
			List temp  = (List<Employee>) query.list();
			if(temp != null && temp.size() > 0){
				employee = (Employee) temp.get(0);
			}
			session.getTransaction().commit();
			return employee;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}

	public void createEmployeeAndReporting(
			List<EmployeeAndReporting> employeeAndReportingsList) {
		System.out.println("IN EMPLOYEE AND REPORTING DATA EXPORTING");
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			for (EmployeeAndReporting employeeAndReporting : employeeAndReportingsList) {
				session.save(employeeAndReporting);
			}
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();

			// TODO: handle exception
		}
	}

	public Employee getProjectManagerByEmployeeId(String employeeCode) {
		System.out.println("IN EMPLOYEE BY EMPLOYEEID DATA IMPORTING PM");
		Employee employee = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from Employee e where e.employeeCode =:employeeCode";
			Query query = session.createQuery(queryString).setString(
					"employeeCode", employeeCode);
			List temp = (List<Employee>) query.list();
			if(temp != null && temp.size() > 0){
				employee = (Employee) temp.get(0);
			}
			
			 session.getTransaction().commit();
			return employee;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}

	public Employee getCompetencyLeadByEmployeeId(String employeeCode) {
		System.out.println("IN EMPLOYEE BY EMPLOYEEID DATA IMPORTING CL");
		Employee employee = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from Employee e where e.employeeCode =:employeeCode";
			Query query = session.createQuery(queryString).setString(
					"employeeCode", employeeCode);
			List temp  = (List<Employee>) query.list();
			if(temp != null && temp.size() > 0){
				employee = (Employee) temp.get(0);
			}
			session.getTransaction().commit();
			return employee;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}

	public Employee getRegularEmployeeByEmployeeId(String employeeCode) {
		System.out.println("IN EMPLOYEE BY EMPLOYEEID DATA IMPORTING Em");
		Employee employee = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from Employee e where e.employeeCode =:employeeCode";
			Query query = session.createQuery(queryString).setString(
					"employeeCode", employeeCode);
			List temp = (List<Employee>) query.list();
			if(temp != null && temp.size() > 0){
				employee = (Employee) temp.get(0);
			}
			session.getTransaction().commit();
			return employee;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}

	public static void main(String a[]) {
		System.out.println(new DataMigrationEngineDAO().getFlexUser("E50001"));
	}

}
